package com.tengju.bff.interfaces.manage.crm.dealer;

import com.tengju.bff.interfaces.manage.crm.dealer.facade.CrmDealerInfoFacade;
import com.tengju.bff.interfaces.manage.crm.dealer.form.CrmDealerInfoExportForm;
import com.tengju.bff.interfaces.manage.crm.dealer.form.CrmDealerInfoQueryForm;
import com.tengju.bff.interfaces.shared.ApiResponse;
import com.tengju.bff.interfaces.shared.KVVO;
import com.tengju.bff.interfaces.shared.TableVO;
import com.tengju.bff.interfaces.shared.export.DealerInfoExportQuery;
import com.tengju.bff.interfaces.shared.servlet.ManageEntrance;
import com.tengju.bff.interfaces.shared.servlet.ServletContextUtil;
import com.tengju.export.application.ExportService;
import com.tengju.export.domain.record.ExportDataType;
import com.tengju.export.domain.record.ExportFileType;
import com.tengju.user.domain.model.user.UserIdCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * @Author: 李大仙
 * @Date: 2021/4/21 2:28 下午
 */
@Validated
@RestController
@Api(tags = "总监CRM-经销商信息")
@RequestMapping(value = "manage/crm/dealer")
@Slf4j
@ManageEntrance
@RequiredArgsConstructor(onConstructor_ = {@Autowired})
public class DealerInfoController {

    private final CrmDealerInfoFacade crmDealerInfoFacade;
    private final ExportService exportService;

    @Value("${feishu.wx.role.code}")
    String feishuWxRoleCode;

    @ApiOperation(value = "CRM获取经销商信息", notes = "CRM获取经销商信息")
    @RequestMapping(value = "/getCrmDealerInfoList", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ApiResponse<TableVO> getCrmDealerInfo(CrmDealerInfoQueryForm crmDealerInfoQueryForm) {
        TableVO vo = crmDealerInfoFacade.queryCrmDealerInfos(crmDealerInfoQueryForm);
        return ApiResponse.newSuccess(vo);
    }

    @ApiOperation(value = "CRM获取经销商上级信息", notes = "CRM获取经销商上级信息")
    @RequestMapping(value = "/getCrmDealerSupInfo", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ApiResponse<KVVO> getCrmDealerSupInfo(@NotNull @RequestParam("idCode") Long idCode) {

        return ApiResponse.newSuccess(crmDealerInfoFacade.getCrmDealerSupInfo(idCode));
    }

    @ApiOperation(value = "CRM导出经销商信息列表", notes = "CRM导出经销商信息列表")
    @RequestMapping(value = "/export", method = RequestMethod.GET)
    public ApiResponse<Object> asyncExport(CrmDealerInfoExportForm crmDealerInfoExportForm) {
        long staffId = ServletContextUtil.getServletContext().getStaffId().getValue();
        String realName = ServletContextUtil.getServletContext().getRealName();
        DealerInfoExportQuery exportQuery = new DealerInfoExportQuery();
        List<String> roleCodes = ServletContextUtil.getServletContext().getRoleCodes();
        if (roleCodes.contains(feishuWxRoleCode)){
            exportService.exportData("企业微信经销商信息列表", ExportFileType.XLSX,
                    "wechatDealerInfoExportDataSourceRepository",
                    ExportDataType.TABLE, exportQuery,
                    staffId, realName);
        } else {
            List<UserIdCode> genSecIdCodes = ServletContextUtil.getServletContext().getGenSecIdCodes();
            exportQuery.setGenSecIdCodes(genSecIdCodes);
            exportQuery.setIdCode(crmDealerInfoExportForm.getIdCode());
            exportQuery.setLevel(crmDealerInfoExportForm.getLevel());
            exportQuery.setStartTime(crmDealerInfoExportForm.getStartTime());
            exportQuery.setEndTime(crmDealerInfoExportForm.getEndTime());
            exportQuery.setAreaSupIdCode(crmDealerInfoExportForm.getAreaSupIdCode());
            exportQuery.setMobile(crmDealerInfoExportForm.getMobile());
            exportQuery.setNickName(crmDealerInfoExportForm.getNickName());
            exportQuery.setInviteCode(crmDealerInfoExportForm.getInviteCode());
            exportQuery.setWeChat(crmDealerInfoExportForm.getWeChat());
            exportQuery.setGoodNumber(crmDealerInfoExportForm.getGoodNumber());
            exportService.exportData("经销商信息列表", ExportFileType.XLSX,
                    "dealerInfoExportDataSourceRepository",
                    ExportDataType.TABLE, exportQuery,
                    staffId, realName);
        }
        return ApiResponse.newSuccess();
    }
}
